<?php
/**
 * @file polymercontroller.php
 * @author yuebin
 * @date 2016-08-12 11:04:45
 * @brief 聚合控件
 */
namespace haibao\cms\business\control\pc\channel\polymer;
use haibao\cms\data\control\PolymerController as PolymerControllerData ;

use haibao\cms\business\control\pc\action\ArticleAction;
use haibao\cms\model\data\ArticleProImage as ArticleProImageModel;
use haibao\cms\business\control\FeatureController;
use \haibao\cms\model\data\PolymerInfo as PolymerInfoModel;
use haibao\frame\data\query\Condition;

class PolymerController extends \haibao\cms\business\control\pc\channel\ChannelController{


	const STYLE_MODEL_8_4 = 's8_4';
	const STYLE_MODEL_4_8 = 's4_8';
	const STYLE_MODEL_4_6 = 's4_6';
	const STYLE_MODEL_4_3 = 's4_3';
	const STYLE_MODEL_4_4 = 's4_4';
	
	public static $PAGE_SIZE = 100;
	public static $HTML_SIZE = 7;
	public static $DEFAULT_IMAGE_URL = array('http://c1.haibao.cn/img/600_0_100_1/1471224265.8952/2ec68b4f054f3a139f74857b931d2b17.jpg','http://c1.haibao.cn/img/600_0_100_1/1471224275.8847/0ee0fa107c4a85de3fc7c4cf1b81a256.jpg');
	public static $ANIMATION_EFFECTS = array(
			'b-link-flow',
			'b-link-stroke',
			'b-link-flow',
			'b-link-box',
			'b-link-stripe',
			'b-link-apart-vertical',
			'b-link-apart-horisontal');
	public $articleAction;
	private static $_instance;
	private $polymerData;
	public function __construct($updateTime='',$controlId='',$isPublish=''){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	public function __clone(){
	}
	
	public static function getPolymerInstance(){
		if(!(self::$_instance instanceof self)){
			self::$_instance = new self;
			self::$_instance->polymerData = new PolymerControllerData;
			self::$_instance->articleAction = ArticleAction::initArticleAction();
		}
		return self::$_instance;
	}
	
	public function getPolymerInfoList($status,$limit=0){
		return $this->polymerData->getPolymerInfoList($status,$limit);
    }
    

    public function formatTagToSegment($tagList){
    	if($tagList){
    		return explode('|', $tagList);
    	}
    	return array();
    }
    
    public function formatTagToSingle($segmentTagList){
    	 if($segmentTagList){
	    	return array_reduce($segmentTagList, function($result, $w){
	    		if(!$result) $result = array();
	    		$w = array_filter(explode('#',$w));
	    		return array_unique(array_merge($result,$w));
	    	});
    	 }
    	 return array();
    }

    /**
     * 获取聚合标签分页数据
     * @param Integer $tagId
     * @return Array
     */
    public function getPolymerTagList($tagId,$limit=1){
    	return $this->polymerData->getPolymerTagList($tagId,$limit);
    }
    
    /**
     * 获取聚合标签总数据
     * @param Integer $tagId
     * @return Integer
     */
    public function getPolymerInfoCnt($tagId,$status=0){
    	return $this->polymerData->getPolymerInfoCnt($tagId,$status);
    }
    
    /**
     * 获取聚合数据区间数量
     * @param Integer $tagId
     * @return Integer
     */
    public function getPolymerInfoCntBetweenId($tagId,$startId,$endId){
    	return $this->polymerData->getPolymerInfoCntBetweenId($tagId,$startId,$endId);
    }
    
    public function getPolymerInfoByTagId($tagId,$status=0){
    	return $this->polymerData->getPolymerInfoByTagId($tagId,$status);
    }
    

    public function getPolymerInfoBetweenId($tagId,$startId,$endId,$notEqualId = 0){
    	return $this->polymerData->getPolymerInfoBetweenId($tagId,$startId,$endId,$notEqualId);
    }
    

    /**
     * 第一个中最后一个布局不满足 self::$HTML_SIZE 需要获取第二页补齐
     * @param Integer $tagId
     * @param Integer $lastPolymerInfoId
     * @param Integer $limit
     */
    public function getCompletePolymerInfoList($tagId, $lastPolymerInfoId, $limit){
    	$list = $this->polymerData->getCompletePolymerInfoList($tagId, $lastPolymerInfoId, $limit);
    	$list = $this->getPolymerInfoRelData(array($list));
    	$list = array_shift($list);
    	return $list;
    }

    public function updatePolymerInfoStatus($polymerInfoId, $status = PolymerInfoModel::STATUS_OK){
    	$this->polymerData->updatePolymerInfoStatus($polymerInfoId, $status);
    }
    
    public function getPolymerTagBetweenTagIdAndPolymerId($tagId,$polymerInfoId){
    	return $this->polymerData->getPolymerTagBetweenTagIdAndPolymerId($tagId, $polymerInfoId);
    }
    
    public function slicPage($polymerInfoCnt){
    	/* if($polymerInfoCnt < self::$PAGE_SIZE){
    		return array(1=>$polymerInfoCnt);
    	}
    	$everyPageNumArr = array();
    	$pageCount = floor($polymerInfoCnt/self::$PAGE_SIZE);
    	$lastPageNum = $polymerInfoCnt%self::$PAGE_SIZE;
    	for($i=1;$i<=$pageCount;$i++){
    		$everyPageNumArr[$i] = self::$PAGE_SIZE;
    		if($lastPageNum && ($i==$pageCount)){
    			$everyPageNumArr[$i] = self::$PAGE_SIZE + $lastPageNum;
    		}
    	}
    	return $everyPageNumArr; */
    	if($polymerInfoCnt < self::$PAGE_SIZE){
    	 return array(1=>$polymerInfoCnt);
    	}
    	$everyPageNumArr = array();
    	$pageCount = ceil($polymerInfoCnt/self::$PAGE_SIZE);
    	$lastPageNum = $polymerInfoCnt%self::$PAGE_SIZE;
    	for($i=1;$i<=$pageCount;$i++){
    		$everyPageNumArr[$i] = self::$PAGE_SIZE;
	    	if($lastPageNum && ($i==$pageCount)){
	    		$everyPageNumArr[$i] = $lastPageNum;
	    	}
    	}
    	return $everyPageNumArr;
    }
    
    // TODO : 原方法调用
	public function packageHtml($polymerInfoList,$tagId,$isKey = false){

		$this->getPolymerInfoRelData($polymerInfoList);
		foreach($polymerInfoList as $pageNum=>$polymerInfo){
			$polymerInfo = array_chunk($polymerInfo,self::$HTML_SIZE);
			foreach($polymerInfo as $key=>$info){
				$htmlStyle = $this->getArrangeEnum();
				if(count($info)!=self::$HTML_SIZE){
					$lastPolymerInfo = end($info);
					$commpleteNum = self::$HTML_SIZE-count($info);
					$completePolymerInfoList = array();
					if(!$isKey){
						$completePolymerInfoList = $this->getCompletePolymerInfoList($tagId,(int)$lastPolymerInfo->Id,$commpleteNum);
					}
					if($completePolymerInfoList){
						$info = array_merge($info,$completePolymerInfoList);
					}else{
						$info = array_pad($info, self::$HTML_SIZE,'');
						//$htmlStyle = array_slice($htmlStyle, 0,self::$HTML_SIZE-$commpleteNum);
					}
				}
				$polymerInfo[$key] = array_map('self::packageInfo',$info,$htmlStyle);

			}
			$polymerInfoList[$pageNum] = $polymerInfo;
		}
		return $polymerInfoList;
    }
    
    /**
     * 获取主频道下聚合数据最多的标签列表
     */
	public function getNotesTagListByTopTagId($topTagIds,$limit = 1){
		if(is_array($topTagIds)){
			$list = array();
			foreach($topTagIds as $tagId){
				$objectInfo = $this->polymerData->getNotesTagListByTopTagId($tagId,$limit);
				$list = array_merge($list,$objectInfo);
			}
			return $this->getPolymerInfoRelData(array("1"=>$list));
		}else{
			$objectInfo = $this->polymerData->getNotesTagListByTopTagId($topTagIds,$limit);
			return $this->getPolymerInfoRelData(array("1"=>$objectInfo));
		}
	}
    /**
     * 获取主频道下聚合数据最多的标签列表
     */
    public function getPolymerListByParentTagId($topTadId,$objectType = array(),$limit=1){
    	return $this->polymerData->getPolymerListByParentTagId($topTadId,$objectType,$limit);
    }
    /**
     * 获取聚合内容相关数据(url/导读图)
     * @param Array $polymerInfoList
     * @return 由于数据查询量大 存入缓存以便下次使用
     */
    private function getPolymerInfoRelData($polymerInfoList,$tagId = 0){
    	 
    	//\haibao\cms\cache\Polymer::delAllProImage();
    	//$relProImage = \haibao\cms\cache\Polymer::getAllProImage();
    	$relProImage = array();
    	$needProImageArr = array(
    			PolymerInfoModel::OBJECT_TYPE_ARTICLE=>array(),
    			PolymerInfoModel::OBJECT_TYPE_IMAGE=>array(),
    			PolymerInfoModel::OBJECT_TYPE_VIDEO=>array(),
    			PolymerInfoModel::OBJECT_TYPE_FEATURE=>array(),
    	);
    	foreach($polymerInfoList as $pageNum=>$list){
    
    		if(is_array($list)){
	    		foreach($list as $key=>$polymer){
	    			
	    			if($polymer && isset($polymer->TagList)){
		    			$tagList = $this->formatTagToSingle($this->formatTagToSegment($polymer->TagList));
		    			$polymer->TagInfoList = $this->getTagsById($tagList);
		    			if(isset($relProImage[$polymer->ObjectType][$polymer->ObjectId])){
		    				$polymer->ProImage = $relProImage[$polymer->ObjectType][$polymer->ObjectId];
		    			}else{
		    				array_push($needProImageArr[$polymer->ObjectType], (int)$polymer->ObjectId);
		    			}
	    			}
	    		}
    		}
    
    	}
    	if($needProImageArr[PolymerInfoModel::OBJECT_TYPE_ARTICLE]){
    		if(!isset($relProImage[PolymerInfoModel::OBJECT_TYPE_ARTICLE])) $relProImage[PolymerInfoModel::OBJECT_TYPE_ARTICLE] = array();
    		$relProImage[PolymerInfoModel::OBJECT_TYPE_ARTICLE] = $relProImage[PolymerInfoModel::OBJECT_TYPE_ARTICLE] + $this->articleAction->getArticleProImage($needProImageArr[PolymerInfoModel::OBJECT_TYPE_ARTICLE],array(ArticleProImageModel::Level_FOUR,ArticleProImageModel::Level_FIVE,ArticleProImageModel::Level_SIX));
    	}
    	
    	if($needProImageArr[PolymerInfoModel::OBJECT_TYPE_IMAGE]){
    		if(!isset($relProImage[PolymerInfoModel::OBJECT_TYPE_IMAGE])) $relProImage[PolymerInfoModel::OBJECT_TYPE_IMAGE] = array();
    		$relProImage[PolymerInfoModel::OBJECT_TYPE_IMAGE] = $relProImage[PolymerInfoModel::OBJECT_TYPE_IMAGE] + $this->articleAction->getCollProImage($needProImageArr[PolymerInfoModel::OBJECT_TYPE_IMAGE],array(ArticleProImageModel::Level_FOUR,ArticleProImageModel::Level_FIVE,ArticleProImageModel::Level_SIX));
    	}
    
    	if($needProImageArr[PolymerInfoModel::OBJECT_TYPE_VIDEO]){
    		if(!isset($relProImage[PolymerInfoModel::OBJECT_TYPE_VIDEO])) $relProImage[PolymerInfoModel::OBJECT_TYPE_VIDEO] = array();
    		$videoList = \haibao\cms\business\Video::getVideoByids($needProImageArr[PolymerInfoModel::OBJECT_TYPE_VIDEO]);
    		if($videoList){
    			$tempVideoPro = array();
    			foreach($videoList as $video){
    				$tempVideoPro[$video->Id][ArticleProImageModel::Level_FOUR] = $video->SquarePicUrl;
    				$tempVideoPro[$video->Id][ArticleProImageModel::Level_FIVE] = $video->ProPicUrl;
    			}
    			$relProImage[PolymerInfoModel::OBJECT_TYPE_VIDEO] = $relProImage[PolymerInfoModel::OBJECT_TYPE_VIDEO] + $tempVideoPro;
    		}
    	}
    	 
    	if($needProImageArr[PolymerInfoModel::OBJECT_TYPE_FEATURE]){
    		if(!isset($relProImage[PolymerInfoModel::OBJECT_TYPE_FEATURE])) $relProImage[PolymerInfoModel::OBJECT_TYPE_FEATURE] = array();
    
    		$featureList = FeatureController::getFeatureListById($needProImageArr[PolymerInfoModel::OBJECT_TYPE_FEATURE]);
    		if($featureList){
    			$tempFeaturePro = array();
    			foreach($featureList as $feature){
    				$tempFeaturePro[$feature->FeatureDefineId]['ProImage'] = $feature->Image->Filename;
    				$tempFeaturePro[$feature->FeatureDefineId]['ChannelUrl'] = $feature->Url;
    			}
    			$relProImage[PolymerInfoModel::OBJECT_TYPE_FEATURE] = $relProImage[PolymerInfoModel::OBJECT_TYPE_FEATURE] + $tempFeaturePro;
    		}
    	}

    	foreach($polymerInfoList as $pageNum=>$list){
    		foreach($list as $key=>$polymer){
    			if($polymer && !isset($polymer->ProImage)){
    				if(isset($relProImage[$polymer->ObjectType][$polymer->ObjectId])){
    					$polymer->ProImage = $relProImage[$polymer->ObjectType][$polymer->ObjectId];
    				}else{
    					$polymer->ProImage = '';
    					$relProImage[$polymer->ObjectType][$polymer->ObjectId] = '';
    				}
    			}
    		}
    	}
    	/* if(count($needProImageArr,1)>4){
    		\haibao\cms\cache\Polymer::setAllProImage(json_encode($relProImage));
    	} */
    	return $polymerInfoList;
    }

    private static function packageInfo($info, $htmlStyle){
        
    	if(!$info) return $htmlStyle;
    	$info->ClassName = $htmlStyle;
    
    	if($htmlStyle == self::STYLE_MODEL_4_3){
    		$proLevel = ArticleProImageModel::Level_FIVE;
    	}elseif($htmlStyle == self::STYLE_MODEL_4_4){
    		$proLevel = ArticleProImageModel::Level_FOUR;
    	}elseif($htmlStyle == self::STYLE_MODEL_4_8){
    		$proLevel = ArticleProImageModel::Level_FOUR;
    	}elseif($htmlStyle == self::STYLE_MODEL_8_4){
    		$proLevel = ArticleProImageModel::Level_FOUR;
    	}elseif($htmlStyle == self::STYLE_MODEL_4_6){
    		$proLevel = ArticleProImageModel::Level_FIVE;
    	}
    	if(is_object($info->ProImage)){
    		$info->ProImage = json_decode(json_encode($info->ProImage),true);
    	}
    	switch($info->ObjectType){
    		case PolymerInfoModel::OBJECT_TYPE_ARTICLE:
    			$proImage = $info->ProImage;
    			$info->ProImage = isset($proImage[$proLevel])?$proImage[$proLevel]:
    			(isset($proImage[ArticleProImageModel::Level_ZERO])?$proImage[ArticleProImageModel::Level_ZERO]:'');
    			$info->ChannelUrl = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL).'/notes/article/'.$info->PublishId.'.htm';
    			break;
    		case PolymerInfoModel::OBJECT_TYPE_IMAGE:
    			$proImage = $info->ProImage;
    			$info->ProImage = isset($proImage[$proLevel])?$proImage[$proLevel]:
    			(isset($proImage[ArticleProImageModel::Level_ZERO])?$proImage[ArticleProImageModel::Level_ZERO]:'');
    
    			$info->ChannelUrl = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/www/pics/'.$info->ObjectId.'.htm';
    			break;
    		case PolymerInfoModel::OBJECT_TYPE_VIDEO:
    		    if(is_array($info->ProImage)){
    		        $proImage = $info->ProImage;
    		        $info->ProImage = isset($proImage[$proLevel]) ? $proImage[$proLevel] : '';
    		    }
    			$info->ChannelUrl = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_VIDEO).'/play/'.$info->ObjectId.'.htm';
    			break;
    		case PolymerInfoModel::OBJECT_TYPE_FEATURE:
    			if($info->ProImage){
    				$info->ChannelUrl = isset($info->ProImage['ChannelUrl'])?$info->ProImage['ChannelUrl']:'';
    				$info->ProImage = $info->ProImage['ProImage'];
    			}else{
    				$info->ChannelUrl = $info->ProImage = '';
    			}
    			break;
    		case PolymerInfoModel::OBJECT_TYPE_H5:
    			$info->ChannelUrl = '';
    			break;
    		default:
    			$info->ChannelUrl = '';
    			break;
    	}
    	
		if($htmlStyle == self::STYLE_MODEL_4_3){
			$width = 480;
			$height = 360;
			if($info->ProImage){
				$info->ProImage = self::getImageLink($info->ProImage,$width,$height);
			}
		}elseif($htmlStyle == self::STYLE_MODEL_4_6){
			$width = 480;
			$height = 360;
			if($info->ProImage){
				$info->ProImage = self::getImageLink($info->ProImage,$width,$height);
			}
		}else{
			$width = 400;
			$height = 400;
			if($info->ProImage){
				$info->ProImage = self::getImageLink($info->ProImage,$width,$height);
			}else{
				$info->ProImage = self::$DEFAULT_IMAGE_URL[array_rand(self::$DEFAULT_IMAGE_URL)];
			}
		}
		$info->Animation = self::$ANIMATION_EFFECTS[array_rand(self::$ANIMATION_EFFECTS)];
    	return $info;
    }
    
    private function getArrangeEnum(){
    	$finalArr = array(
    			array('s8_4','s4_6','s4_8','s4_4','s4_3','s4_4','s4_3'),
    			array('s4_6','s8_4','s4_8','s4_4','s4_3','s4_4','s4_3'),
    			array('s4_4','s4_8','s4_3','s4_6','s4_4','s8_4','s4_3'),
    			array('s4_3','s4_4','s4_8','s4_3','s4_4','s4_6','s8_4'),
    
    			array('s8_4','s4_6','s4_4','s4_8','s4_3','s4_4','s4_3'),
    			array('s4_6','s8_4','s4_4','s4_8','s4_3','s4_4','s4_3'),
    			array('s4_8','s4_4','s4_3','s4_6','s4_4','s8_4','s4_3'),
    			array('s4_3','s4_8','s4_4','s4_3','s4_4','s4_6','s8_4'),
    
    			array('s8_4','s4_3','s4_6','s4_8','s4_4','s4_4','s4_3'),
    			array('s4_3','s8_4','s4_6','s4_8','s4_4','s4_4','s4_3'),
    			array('s4_4','s4_8','s4_6','s4_4','s4_3','s8_4','s4_3'),
    			array('s4_3','s4_4','s4_8','s4_6','s4_4','s8_4','s4_3'),
    
    			array('s8_4','s4_3','s4_3','s4_4','s4_8','s4_6','s4_4'),
    			array('s4_3','s8_4','s4_3','s4_4','s4_8','s4_6','s4_4'),
    			array('s4_8','s4_4','s4_3','s4_3','s4_4','s4_6','s8_4'),
    			array('s4_3','s4_8','s4_4','s4_3','s4_4','s4_6','s8_4'),
    
    			array('s8_4','s4_3','s4_3','s4_8','s4_4','s4_6','s4_4'),
    			array('s4_6','s8_4','s4_4','s4_8','s4_3','s4_4','s4_3'),
    			array('s4_8','s4_4','s4_6','s4_4','s4_3','s8_4','s4_3'),
    			array('s4_6','s4_4','s4_8','s4_4','s4_3','s8_4','s4_3'),
    
    			array('s8_4','s4_3','s4_6','s4_4','s4_8','s4_4','s4_3'),
    			array('s4_3','s8_4','s4_3','s4_8','s4_4','s4_6','s4_4'),
    			array('s4_4','s4_8','s4_3','s4_3','s4_4','s4_6','s8_4'),
    			array('s4_6','s4_8','s4_4','s4_4','s4_3','s8_4','s4_3')
    	);
    	 
    	return $finalArr[array_rand($finalArr)];
    
    	/*
    	 $leftArr = array('s4_6','s4_3','s4_3');
    	shuffle($leftArr);
    	// 1表示left 2 表示右
    	$arrangeArr = ARRAY(
    			1=>ARRAY('arrange'=>array(1,2,2,1,2,1,2),'right'=>array(array(4,3,3,1),array(3,4,3,1))),
    			2=>ARRAY('arrange'=>array(1,2,1,2,2,1,2),'right'=>array(array(1,3,4,3),array(1,4,3,3))),
    			3=>ARRAY('arrange'=>array(2,1,2,2,1,2,1),'right'=>array(array(1,3,4,3),array(1,4,3,3))),
    			4=>ARRAY('arrange'=>array(2,2,1,2,1,2,1),'right'=>array(array(4,3,3,1),array(3,4,3,1))),
    			1=>ARRAY('arrange'=>array(1,2,2,1,2,1,2),'right'=>array(array('s4_8','s4_4','s4_4','s8_4'),array('s4_4','s4_8','s4_4','s8_4'))),
    			2=>ARRAY('arrange'=>array(1,2,1,2,2,1,2),'right'=>array(array('s8_4','s4_4','s4_8','s4_4'),array('s8_4','s4_8','s4_4','s4_4'))),
    			3=>ARRAY('arrange'=>array(2,1,2,2,1,2,1),'right'=>array(array('s8_4','s4_4','s4_8','s4_4'),array('s8_4','s4_8','s4_4','s4_8'))),
    			4=>ARRAY('arrange'=>array(2,2,1,2,1,2,1),'right'=>array(array('s4_8','s4_4','s4_4','s8_4'),array('s4_4','s4_8','s4_4','s8_4'))),
    	);
    	$arrangeArr = $arrangeArr[array_rand($arrangeArr)];
    	$arrange = $arrangeArr['arrange'];
    	$rightArr = $arrangeArr['right'][array_rand($arrangeArr['right'])];
    	$finalArr = array();
    	foreach($arrange as $key=>$val){
    	if($val==1){
    	array_push($finalArr,array_shift($leftArr));
    	}else{
    	array_push($finalArr,array_shift($rightArr));
    	}
    	}
    	return $finalArr;
    	*/
    }
    
    
   	public function packageProImage($offset , $limit){

   		//\haibao\cms\cache\Polymer::delAllProImage();
   		$relProImage = \haibao\cms\cache\Polymer::getAllProImage();
   		if(!$relProImage){
   			$relProImage = array(
   					PolymerInfoModel::OBJECT_TYPE_ARTICLE=>array(),
   					PolymerInfoModel::OBJECT_TYPE_IMAGE=>array(),
   					PolymerInfoModel::OBJECT_TYPE_VIDEO=>array(),
   					PolymerInfoModel::OBJECT_TYPE_FEATURE=>array(),
   			);
   		}else{
   			/* print_r($relProImage);
   			echo count($relProImage);
   			echo count($relProImage,1);
   			exit; */
   		}
   		$sql = "SELECT Id,ObjectId,ObjectType FROM `PolymerInfo` order by Id desc limit $offset,$limit;";
   		$result = $this->polymerData->query($sql);
   		$queryData = array();
   		while($result->fetch_assoc()){
   			$objectId = $result->getData('ObjectId');
   			$objectType = $result->getData('ObjectType');
   			$queryData[$result->getData('Id')] = array('ObjectId'=>$objectId,'ObjectType'=>$objectType);
   			echo '.';
   			switch($objectType){
   				case PolymerInfoModel::OBJECT_TYPE_ARTICLE:
   					if(!isset($relProImage[PolymerInfoModel::OBJECT_TYPE_ARTICLE][$objectId])){
   					 	$proImage = $this->articleAction->getArticleProImage(array($objectId),array(ArticleProImageModel::Level_FOUR,ArticleProImageModel::Level_FIVE,ArticleProImageModel::Level_SIX));
   					 	$relProImage[PolymerInfoModel::OBJECT_TYPE_ARTICLE] = $relProImage[PolymerInfoModel::OBJECT_TYPE_ARTICLE] + $proImage;
   					}
   					break;
   				case PolymerInfoModel::OBJECT_TYPE_IMAGE:
   					if(!isset($relProImage[PolymerInfoModel::OBJECT_TYPE_IMAGE][$objectId])){
   						$proImage = $this->articleAction->getCollProImage(array($objectId),array(ArticleProImageModel::Level_FOUR,ArticleProImageModel::Level_FIVE,ArticleProImageModel::Level_SIX));
   						$relProImage[PolymerInfoModel::OBJECT_TYPE_IMAGE] = $relProImage[PolymerInfoModel::OBJECT_TYPE_IMAGE] + $proImage;
   					}
   					break;
   				case PolymerInfoModel::OBJECT_TYPE_VIDEO:
   					if(!isset($relProImage[PolymerInfoModel::OBJECT_TYPE_VIDEO][$objectId])){
	   					$videoList = \haibao\cms\business\Video::getVideoByids(array($objectId));
	   					if($videoList){
	   						$tempVideoPro = array();
	   						foreach($videoList as $video){
	   						    $tempVideoPro[$video->Id][ArticleProImageModel::Level_FOUR] = $video->SquarePicUrl;
	   						    $tempVideoPro[$video->Id][ArticleProImageModel::Level_FIVE] = $video->ProPicUrl;
	   						}
	   						$relProImage[PolymerInfoModel::OBJECT_TYPE_VIDEO] = $relProImage[PolymerInfoModel::OBJECT_TYPE_VIDEO] + $tempVideoPro;
	   					}
   					}
   					break;
   				case PolymerInfoModel::OBJECT_TYPE_FEATURE:
   					if(!isset($relProImage[PolymerInfoModel::OBJECT_TYPE_FEATURE][$objectId])){
	   					$featureList = FeatureController::getFeatureListById(array($objectId));
	   					if($featureList){
	   						$tempFeaturePro = array();
	   						foreach($featureList as $feature){
	   							$tempFeaturePro[$feature->FeatureDefineId]['ProImage'] = $feature->Image->Filename;
	   							$tempFeaturePro[$feature->FeatureDefineId]['ChannelUrl'] = $feature->Url;
	   						}
	   						$relProImage[PolymerInfoModel::OBJECT_TYPE_FEATURE] = $relProImage[PolymerInfoModel::OBJECT_TYPE_FEATURE] + $tempFeaturePro;
	   					}
   					}
   					break;
   			}
   			
   		}
   		
   		foreach($queryData as $data){
   			if(!isset($relProImage[$data['ObjectType']][$data['ObjectId']])){
   				$relProImage[$data['ObjectType']][$data['ObjectId']] = '';
   			}
   		}
    	\haibao\cms\cache\Polymer::setAllProImage(json_encode($relProImage));
    }
    
    public function packageNewHtml($polymerInfoList,$tagId,$isKey = false){
    
    	$this->getPolymerInfoRelData($polymerInfoList,$tagId);
    	return $polymerInfoList;
    }
    
    public function slicePolymerList($polymerInfoList){
    	$polymerInfoList = array_chunk($polymerInfoList,self::$HTML_SIZE);
    	$firstList = json_decode(json_encode($polymerInfoList[0]));
    	foreach($polymerInfoList as $key=>$info){
    		$htmlStyle = $this->getArrangeEnum();
    		if(count($info)!=self::$HTML_SIZE){
    			$randArr = array_slice($firstList, 0,self::$HTML_SIZE-count($info));
    			shuffle($randArr);
    			$info = json_decode(json_encode(array_merge($info,$randArr)));
    			//$info = array_pad($info, self::$HTML_SIZE,'');
    		}
    		$polymerInfoList[$key] = array_map('self::packageInfo',$info,$htmlStyle);
    	}
    	return $polymerInfoList;
    }
    public function deletePolymerInfoById($id){
    	$this->polymerData->deletePolymerInfoById((int)$id);
    }
    
    private function getTagsById($tagIds){
    	if(!$tagIds) return array();
    	return $this->polymerData->getTagsById($tagIds);
    }
    
    public function getTagList($polymerInfoList,$primaryTagList){
    	
    	$topTagArr = self::getTopTags();
    	
    	foreach($polymerInfoList as $polymerInfo){

	    	if(is_object($polymerInfo->ProImage)){
	    		$polymerInfo->ProImage = json_decode(json_encode($polymerInfo->ProImage),true);
	    	}
	    	if(in_array($polymerInfo->ObjectType,array(PolymerInfoModel::OBJECT_TYPE_ARTICLE,PolymerInfoModel::OBJECT_TYPE_IMAGE))){
	    		if(isset($primaryTagList[$polymerInfo->PrimaiyTagId])){
	    			$polymerInfo->PrimaryTagName = $primaryTagList[$polymerInfo->PrimaiyTagId]['TagName'];
	    			$polymerInfo->PrimaryTagUrl = self::getTagUrl($primaryTagList[$polymerInfo->PrimaiyTagId]['Url'], $primaryTagList[$polymerInfo->PrimaiyTagId]['TagType'], $primaryTagList[$polymerInfo->PrimaiyTagId]['TopId'],$polymerInfo->PrimaiyTagId);
	    		}else{
	    			$polymerInfo->PrimaiyTagId = 0;
	    		}
	    	}
	    	
	    	switch($polymerInfo->ObjectType){
	    		case PolymerInfoModel::OBJECT_TYPE_ARTICLE:
	    			$topTagInfo = $topTagArr[$polymerInfo->ParentTagId];
	    			$proImage = $polymerInfo->ProImage;
	    			if(isset($primaryTagList[$polymerInfo->PrimaiyTagId]) && $primaryTagList[$polymerInfo->PrimaiyTagId]['Id'] == 8411){
	    			     $polymerInfo->ProImage = isset($proImage[ArticleProImageModel::Level_SIX])?$proImage[ArticleProImageModel::Level_SIX]:
	    			    (isset($proImage[ArticleProImageModel::Level_ZERO])?$proImage[ArticleProImageModel::Level_ZERO]:'');
	    			     if($polymerInfo->ProImage) $polymerInfo->ProImage = self::getImageLink($polymerInfo->ProImage,300,400);
	    			}else{
	    			    $polymerInfo->ProImage = isset($proImage[ArticleProImageModel::Level_FOUR])?$proImage[ArticleProImageModel::Level_FOUR]:
	    			    (isset($proImage[ArticleProImageModel::Level_ZERO])?$proImage[ArticleProImageModel::Level_ZERO]:'');
	    			    if($polymerInfo->ProImage) $polymerInfo->ProImage = self::getImageLink($polymerInfo->ProImage,400,400);
	    			}
	    			$polymerInfo->ChannelUrl = $topTagInfo['tagchannelurl'].'/article/'.$polymerInfo->PublishId.'.htm';
	    			break;
	    		case PolymerInfoModel::OBJECT_TYPE_IMAGE:
	    			$topTagInfo = $topTagArr[$polymerInfo->ParentTagId];
	    			$proImage = $polymerInfo->ProImage;
	    			$polymerInfo->ProImage = isset($proImage[ArticleProImageModel::Level_FOUR])?$proImage[ArticleProImageModel::Level_FOUR]:
	    			(isset($proImage[ArticleProImageModel::Level_ZERO])?$proImage[ArticleProImageModel::Level_ZERO]:'');
	    			$polymerInfo->ChannelUrl = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/'.$topTagInfo['channel'].'/pics/'.$polymerInfo->ObjectId.'.htm';
	    			if($polymerInfo->ProImage) $polymerInfo->ProImage = self::getImageLink($polymerInfo->ProImage,400,400);
	    			
	    			
	    			break;
	    		case PolymerInfoModel::OBJECT_TYPE_VIDEO:
	    			$polymerInfo->ChannelUrl = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_VIDEO).'/play/'.$polymerInfo->ObjectId.'.htm';
	    			if(is_array($polymerInfo->ProImage)){
	    			    if($polymerInfo->ProImage && isset($polymerInfo->ProImage[ArticleProImageModel::Level_FIVE])) $polymerInfo->ProImage = self::getImageLink($polymerInfo->ProImage[ArticleProImageModel::Level_FIVE],390,256);
	    			}else{
	    			    if($polymerInfo->ProImage) $polymerInfo->ProImage = self::getImageLink($polymerInfo->ProImage,390,256);
	    			}
	    			
	    			break;
	    		case PolymerInfoModel::OBJECT_TYPE_FEATURE:
	    			if($polymerInfo->ProImage){
	    				$polymerInfo->ChannelUrl = isset($polymerInfo->ProImage['ChannelUrl'])?$polymerInfo->ProImage['ChannelUrl']:'';
	    				if($polymerInfo->ProImage['ProImage']){
	    					$polymerInfo->ProImage =  self::getImageLink($polymerInfo->ProImage['ProImage'],340,256);
	    				}else{
	    					$polymerInfo->ProImage = '';
	    				}
	    			}else{
	    				$polymerInfo->ChannelUrl = $polymerInfo->ProImage = '';
	    			}
	    			break;
	    		case PolymerInfoModel::OBJECT_TYPE_H5:
	    			$polymerInfo->ChannelUrl = '';
	    			break;
	    		default:
	    			$polymerInfo->ChannelUrl = '';
	    			break;
			}
	    	if(!$polymerInfo->ProImage) $polymerInfo->ProImage = self::$DEFAULT_IMAGE_URL[array_rand(self::$DEFAULT_IMAGE_URL)];
    	}
    	return $polymerInfoList;
    }
    public function getNewPolymerInfoByPrimaryTagId($tagId,$limit=2){
    	return $this->polymerData->getNewPolymerInfoByPrimaryTagId($tagId,$limit);
    }
} 